package com.runen.infinitemovement.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.fragment.FocusNotificationFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FocusNotificationActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_comment_me)
    TextView tvCommentMe;
    @BindView(R.id.rv_line_comment_me)
    RelativeLayout rvLineCommentMe;
    @BindView(R.id.ll_comment_me)
    LinearLayout llCommentMe;
    @BindView(R.id.tv_prise_me)
    TextView tvPriseMe;
    @BindView(R.id.rv_line_prise_me)
    RelativeLayout rvLinePriseMe;
    @BindView(R.id.ll_prise_me)
    LinearLayout llPriseMe;
    @BindView(R.id.tv_focus_me)
    TextView tvFocusMe;
    @BindView(R.id.rv_line_focus_me)
    RelativeLayout rvLineFocusMe;
    @BindView(R.id.ll_focus_me)
    LinearLayout llFocusMe;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_focus_notification;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("动态通知");
        initViewPager();
    }

    @OnClick({R.id.top_btn_left, R.id.ll_comment_me, R.id.ll_prise_me, R.id.ll_focus_me})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.ll_comment_me:
                viewPager.setCurrentItem(0);
                break;
            case R.id.ll_prise_me:
                viewPager.setCurrentItem(1);
                break;
            case R.id.ll_focus_me:
                viewPager.setCurrentItem(2);
                break;
        }
    }


    private void initViewPager() {
        setFirst();
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                FocusNotificationFragment notificationFragment = new FocusNotificationFragment(position);
                return notificationFragment;
            }

            @Override
            public int getCount() {
                return 3;
            }
        });
//        viewPager.setOffscreenPageLimit(3);
        /**
         * ViewPager切换Fragment,RadioGroup做相应变化
         */
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setFirst();
                        break;
                    case 1:
                        radioGroup.check(R.id.ll_prise_me);
                        rvLineCommentMe.setVisibility(View.INVISIBLE);
                        rvLinePriseMe.setVisibility(View.VISIBLE);
                        rvLineFocusMe.setVisibility(View.INVISIBLE);
                        setTextColor(tvPriseMe, tvCommentMe, tvFocusMe);
                        break;
                    case 2:
                        radioGroup.check(R.id.ll_focus_me);
                        rvLineCommentMe.setVisibility(View.INVISIBLE);
                        rvLinePriseMe.setVisibility(View.INVISIBLE);
                        rvLineFocusMe.setVisibility(View.VISIBLE);
                        setTextColor(tvFocusMe, tvPriseMe, tvCommentMe);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setTextColor(TextView tv1, TextView tv2, TextView tv3) {
        tv1.setTextColor(getResources().getColor(R.color.color_FFF84550));
        tv2.setTextColor(getResources().getColor(R.color.color_FF9B99A9));
        tv3.setTextColor(getResources().getColor(R.color.color_FF9B99A9));
    }

    private void setFirst() {
        radioGroup.check(R.id.ll_comment_me);
        rvLineCommentMe.setVisibility(View.VISIBLE);
        rvLinePriseMe.setVisibility(View.INVISIBLE);
        rvLineFocusMe.setVisibility(View.INVISIBLE);
        setTextColor(tvCommentMe, tvPriseMe, tvFocusMe);
    }

    private Drawable setBounds(int i) {
        Drawable drawable = getResources().getDrawable(i);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        return drawable;
    }

}
